package com.czn.controller.admin;

import com.czn.entity.Drugs;
import com.czn.service.IDrugsService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员对药品管理的控制器
 *
 * @author ChenZeNan
 * @create 2021-04-18 0:03
 */
@Controller
@RequestMapping("/admin/drugs")
public class AdminDrugsController {
    @Autowired
    private IDrugsService drugsService;

    @RequestMapping("/manage")
    public String drugManage(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                             @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                             @RequestParam(required = false) String name,
                             @RequestParam(required = false) Integer type,
                             Model model) {
        PageQuery<Drugs> page = drugsService.findDrugsList(pageNo, pageSize, name, type);
        model.addAttribute("page", page);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("name", name);
        model.addAttribute("type", type);
        model.addAttribute("path", "/admin/drugs/manage");
        StringBuilder sb = new StringBuilder();
        if (name != null) {
            sb.append("&name=" + name);
        }
        if (type != null) {
            sb.append("&type=" + type);
        }
        model.addAttribute("params", sb.toString());
        return "admin/drugsManage";
    }


    /**
     * 跳转到新增药品页面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String toAddFormPage(Model model) {
        model.addAttribute("drugs", new Drugs());
        return "admin/drugsForm";
    }

    /**
     * 跳转到药品修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String toEditFormPage(@PathVariable Integer id, Model model) {
        model.addAttribute("drugs", drugsService.findDrugById(id));
        return "admin/drugsForm";
    }

    /**
     * 新增药品
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody Drugs drug) {
        ResponseResult result = drugsService.addDrug(drug);
        return result;
    }

    /**
     * 编辑药品
     *
     * @param drug
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseResult edit(@RequestBody Drugs drug) {
        ResponseResult result = drugsService.editDrug(drug);
        return result;
    }

    /**
     * 删除药品
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult delete(@PathVariable Integer id) {
        ResponseResult result = drugsService.deleteDrugById(id);
        return result;
    }

}
